package com.finace.money.ui.activity;

import android.content.Intent;
import android.graphics.Color;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bigkoo.pickerview.OptionsPickerView;
import com.finace.money.R;
import com.finace.money.common.Constant;
import com.finace.money.common.util.ACache;
import com.finace.money.common.util.StringUtil;
import com.finace.money.common.util.UIUtil;
import com.finace.money.data.bean.AddressBean;
import com.finace.money.di.component.AppComponent;
import com.finace.money.di.component.DaggerInformationComponent;
import com.finace.money.di.module.InformationModule;
import com.finace.money.presenter.InformationPresenter;
import com.finace.money.presenter.contract.InformationContract;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * Created by zhangpeng on 2018/1/26.
 */

public class InformationActivity extends BaseActivity<InformationPresenter> implements View.OnClickListener, InformationContract.View {
    @BindView(R.id.ll_content)
    LinearLayout llContent;
    @BindView(R.id.rl_left)
    RelativeLayout rlLeft;
    @BindView(R.id.tv_head_title)
    TextView tvHeadTitle;
    @BindView(R.id.et_name)
    EditText etName;
    @BindView(R.id.et_email)
    EditText etEmail;
    @BindView(R.id.et_number_person)
    EditText etNumberPerson;
    @BindView(R.id.tv_address)
    TextView tvAddress;
    @BindView(R.id.rl_address)
    RelativeLayout rlAddress;
    @BindView(R.id.et_address_detail)
    EditText etAddressDetail;
    @BindView(R.id.button_next)
    Button buttonNext;
    private OptionsPickerView pvOptions;

    private List<String> mProvinceList;
    private List<List<String>> mCityList;
    private List<List<List<String>>> mAreaList;

    private int mProvincePosition = 0;
    private int mCityPosition = 0;
    private int mAreaPosition = 0;
    private ACache mACache;

    private String phone;
    private String code;
    private String imgcode;

    private String name;
    private String email;
    private String numberPerson;
    private String address;
    private String addressDetail;


    @Override
    public int setLayout() {
        return R.layout.activity_information;
    }

    @Override
    public void init() {
        mACache = ACache.get(UIUtil.getContext());
        rlLeft.setOnClickListener(this);
        rlAddress.setOnClickListener(this);
        buttonNext.setOnClickListener(this);
        tvHeadTitle.setText(UIUtil.getString(R.string.information));
        initOptionPicker();
        getAcache();
    }

    private void getAcache() {

        mProvinceList = (List<String>) mACache.getAsObject(Constant.ADDRESS_PROVINCE);
        mCityList = (List<List<String>>) mACache.getAsObject(Constant.ADDRESS_CITY);
        mAreaList = (List<List<List<String>>>) mACache.getAsObject(Constant.ADDRESS_AREA);
        phone = getIntent().getStringExtra("phone");
        code = getIntent().getStringExtra("code");
        imgcode = getIntent().getStringExtra("imgcode");
        if (null != mProvinceList && mProvinceList.size() > 0) {
            showAddressView();
        } else {
            mPresenter.getAddress();
        }
    }

    /**
     * 省市级联动
     */
    private void initOptionPicker() {
        pvOptions = new OptionsPickerView.Builder(this, new OptionsPickerView.OnOptionsSelectListener() {
            @Override
            public void onOptionsSelect(int options1, int options2, int options3, View v) {

                mProvincePosition = options1;
                mCityPosition = options2;
                mAreaPosition = options3;
                tvAddress.setText(mProvinceList.get(mProvincePosition) + "-" +
                        mCityList.get(mProvincePosition).get(mCityPosition) + "-" +
                        mAreaList.get(mProvincePosition).get(mCityPosition).get(mAreaPosition));
            }
        })
                .setTitleBgColor(Color.BLACK)
                .setCancelColor(Color.WHITE)
                .setSubmitColor(Color.WHITE)
                .setContentTextSize(14)//设置滚轮文字大小
                .setDividerColor(Color.LTGRAY)//设置分割线的颜色
                .setLineSpacingMultiplier(12f)
                .setSelectOptions(0)//默认选中项
                .setBgColor(Color.BLACK)
                .setCancelColor(Color.GRAY)
                .setSubmitColor(Color.GRAY)
                .setTextColorOut(Color.WHITE)
                .setTextColorCenter(Color.WHITE)
                .isCenterLabel(false) //是否只显示中间选中项的label文字，false则每项item全部都带有label。
                .setBackgroundId(0x66000000) //设置外部遮罩颜色
                .build();

    }

    private void showAddressView() {
        pvOptions.setPicker(mProvinceList, mCityList, mAreaList);
    }

    @Override
    public void setupAcitivtyComponent(AppComponent appComponent) {

        DaggerInformationComponent.builder().appComponent(appComponent)
                .informationModule(new InformationModule(this))
                .build().inject(this);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.rl_left:
                onBackPressed();
                break;
            case R.id.button_next:

                name = etName.getText().toString().trim();
                email = etEmail.getText().toString().trim();
                numberPerson = etNumberPerson.getText().toString().trim();
                address = tvAddress.getText().toString().trim();
                addressDetail = etAddressDetail.getText().toString().trim();
                if (StringUtil.isNullString(name)) {
                    UIUtil.showToastSafe(UIUtil.getString(R.string.name_input));
                    return;
                }
                if (StringUtil.isNullString(email)) {
                    UIUtil.showToastSafe(UIUtil.getString(R.string.email_input));
                    return;
                }
                if (StringUtil.isNullString(numberPerson)) {
                    UIUtil.showToastSafe(UIUtil.getString(R.string.number_person_input));
                    return;
                }
                if (StringUtil.isNullString(tvAddress.getText().toString().trim())) {
                    UIUtil.showToastSafe(UIUtil.getString(R.string.address_input));
                    return;
                }
                if (StringUtil.isNullString(addressDetail)) {
                    UIUtil.showToastSafe(UIUtil.getString(R.string.address_detail_input));
                    return;
                }
                String[] adds = address.split("-");
                Intent intent = new Intent(this, UploadImageActivity.class);
                intent.putExtra("phone",phone);
                intent.putExtra("imgcode",imgcode);
                intent.putExtra("code",code);
                intent.putExtra("name",name);
                intent.putExtra("email",email);
                intent.putExtra("numberPerson",numberPerson);
                intent.putExtra("province",adds[0]);
                intent.putExtra("city",adds[1]);
                intent.putExtra("area",adds[2]);
                intent.putExtra("addressDetail",addressDetail);
                startActivity(intent);
                break;
            case R.id.rl_address:
                chooseAddress();
                break;

        }
    }

    private void chooseAddress() {
        pvOptions.setSelectOptions(mProvincePosition, mCityPosition, mAreaPosition);
        pvOptions.show();
    }

    @Override
    public void showAddress(List<AddressBean> addresss) {
        if (null != addresss && addresss.size() > 0) {
            mProvinceList = new ArrayList<>();
            mCityList = new ArrayList<>();
            mAreaList = new ArrayList<>();
            for (AddressBean addressBean : addresss) {
                mProvinceList.add(addressBean.getName());
                List<AddressBean.CityListBean> cityList = addressBean.getCityList();
                if (null != cityList && cityList.size() > 0) {
                    List<String> citys = new ArrayList<>();
                    List<List<String>> cityAreas = new ArrayList<>();
                    for (AddressBean.CityListBean cityBean : cityList) {
                        citys.add(cityBean.getName());
                        List<AddressBean.CityListBean.AreaListBean> areaList = cityBean.getAreaList();
                        if (null != areaList && areaList.size() > 0) {
                            List<String> areas = new ArrayList<>();
                            for (AddressBean.CityListBean.AreaListBean areaBean : areaList) {
                                areas.add(areaBean.getName());
                            }
                            cityAreas.add(areas);
                        }
                    }
                    mCityList.add(citys);
                    mAreaList.add(cityAreas);
                }
            }
            mACache.put(Constant.ADDRESS_PROVINCE, (Serializable) mProvinceList);
            mACache.put(Constant.ADDRESS_CITY, (Serializable) mCityList);
            mACache.put(Constant.ADDRESS_AREA, (Serializable) mAreaList);
            showAddressView();
        }
    }

}
